//
//  CouponsVc.swift
//  Pods
//
//  Created by eme on 2017/3/29.
//
//
/*
 *优惠券
 */
import UIKit
import UtilCore
import NetWorkCore
import EmptyDataView
import RickonCore

class CouponsVc: Empty_TVc {
    
    /****************************Storyboard UI设置区域****************************/
    var index:Int?
    
    @IBOutlet weak var CustomTableView: UITableView!
    
    
    /*----------------------------   自定义属性区域    ----------------------------*/
    var GetCouponsList:GetCouponsListVm?                            //获取列表ViewModel
    
    var result_Arr:[VouchersModel]?{                                //数据源
        didSet{
            self.CustomTableView.reloadData()
        }
    }
    let hearder = MJRefreshNormalHeader()                           //品牌顶部刷新
    
    /****************************Storyboard 绑定方法区域****************************/
    
    
    
    /**************************** 以下是方法区域 ****************************/
    override func viewDidLoad() {
        super.viewDidLoad()
        self.GetCouponsList?.getCouponsList.onNext("\(index!)")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
        self.customLeftBarButtonItem()
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "使用规则", style: .plain, target: self, action: #selector(CouponsVc.toYHJ_Rules))
        self.CustomTableView.delegate = self
        self.CustomTableView.dataSource = self
        self.CustomTableView.mj_header = hearder
        hearder.setRefreshingTarget(self, refreshingAction:  #selector(CouponsVc.headerfresh))
        self.CustomTableView.dzn_tv_emptyDataSource = self
        self.CustomTableView.dzn_tv_emptyDelegate = self
        self.CustomTableView.tableFooterView = UIView()
    }
    /*
     *  使用规则点击事件
     */
    func toYHJ_Rules(){
        
        _ = "\(UrlManager.sharedInstance.baseUrl)/firstPage/couponuserule.html".openURL()
    }
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        
        self.GetCouponsList = GetCouponsListVm()
        
        self.GetCouponsList?.backDate
            .asObserver()
            .do(onNext:{[unowned self] _ in
                
                self.errorPageView.isHidden = true
                
                }).subscribe(onNext:{ result in
                    
                    self.showEmptyView = true
                    self.result_Arr = result
                
            }).addDisposableTo(disposeBag)
        
        self.GetCouponsList!.loading.asObservable().bindTo(self.view.rx_loading).addDisposableTo(disposeBag)
        self.GetCouponsList!.refresherror.asObserver().bindTo(self.rx_showerrorpage).addDisposableTo(disposeBag)
        self.GetCouponsList!.error.asObserver().bindTo(self.view.rx_error).addDisposableTo(disposeBag)
        self.errorPageView.reloadSubject.bindTo(self.GetCouponsList!.reloadTrigger).addDisposableTo(disposeBag)
        
        
        
//        self.GetCouponsList?.error.asObserver().subscribe(onNext:{error in
//            
//            self.showEmptyView = true
//            self.CustomTableView.reloadData()
//            
//            }).addDisposableTo(disposeBag)
    }
    func headerfresh(){
        
        self.GetCouponsList?.getCouponsList.onNext("\(index!)")
        
        hearder.endRefreshing()
    }
}
extension CouponsVc:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let arr = self.result_Arr{
            return arr.count
        }else{
            return 0
        }
    }
    func numberOfSections(in tableView: UITableView) -> Int{
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! Coupons_TableViewCell
        cell.type = self.index!
        cell.item = self.result_Arr?[indexPath.row]
        if let model = self.result_Arr?[indexPath.row]{
            if index == 1{
                cell.time_Label.text = "\(model.vstart)-\(model.vend)(已使用)"
            }
            else if index == 2
            {
                cell.time_Label.text =  "\(model.vstart)-\(model.vend)(已过期)"
            }else{
                cell.time_Label.text =  "\(model.vstart)-\(model.vend)"
            }
        }
        
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 110
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if index == 1{
            print("跳转使用商品")
        }
    }
}
extension CouponsVc:EmptyDataSource{
    func description(emptyView scrollView: UIScrollView) -> NSAttributedString? {
        return Theme.getEmptyDesc("暂无优惠券~")
    }
    func image(emptyView scrollView: UIScrollView) -> UIImage? {
        return UIImage.init(named: "vouchers_empty", in: AryaCore.bundle, compatibleWith: nil)
    }
    func backgroundColor(emptyView scrollView: UIScrollView) -> UIColor?{
        return UIColor.white
    }
}
